import groovy.text.Template
import groovy.text.SimpleTemplateEngine
import groovy.text.XmlTemplateEngine

class GenerateFile {

	/*生成Rss文件*/
	public static generateRss(List blogList) 
	
	{
			def binding = ["itemList":blogList,"Config":new Config(),"lastUpdateTime":new Date()];
			def engine = new SimpleTemplateEngine()
			def result=engine.createTemplate(DataFactory.rssTemplate).make(binding).toString();
			def outputFile=new File(Config.rssFile);
			outputFile.write(result);
			println "Rss has generated";
			
	}
	
	/*生成网页*/
	
	public static generateWebPage(List blogList,Map categoryMap,List tagList)
	
	{
		int total=blogList.size();
		//生成首页
		Page blogListPage=new Page(totalList:blogList,pageSize:Config.pageSize,prefix:"index");
		blogListPage.initTotalPage();
		for(i in (1..blogListPage.totalPage))
		{	
		 blogListPage.setPageNo(i);
		 generateListPage(blogListPage,categoryMap,total,null,null);
		}
		println "index page has generated";
		
		//生成分类页面
		for (category in categoryMap)
		{
			List categoryList=blogList.findAll{ it.category.equals(category.key) };
			Page categoryListPage=new Page(totalList:categoryList,pageSize:Config.pageSize,prefix:category.value);
			categoryListPage.initTotalPage();
			for(i in (1..categoryListPage.totalPage))
			{
			categoryListPage.setPageNo(i);	
			generateListPage(categoryListPage,categoryMap,total,category.key,null);
			}
		}
		
		println "category page has generated";
		
		//生成Tag页面
		
		for(tag in tagList)
		{
			Page tagPage=new Page(totalList:tag.blogList,pageSize:Config.pageSize,prefix:tag.filePrefix);
			tagPage.initTotalPage();
			for(i in (1..tagPage.totalPage))
			{
			tagPage.setPageNo(i);	
			generateListPage(tagPage,categoryMap,total,null,tag);
			}
			generateListPage(tagPage,categoryMap,total,null,tag);
		}
		
		//生成tag首页
		generateTagListPage(tagList,categoryMap,total);
		
		println "tag page has generated";
		
		//生成detail页面
		for(blog in blogList)
		{
			generateDetailPage(blog,categoryMap,total)
		}
		println "detail page has generated"
		
		//生成search页
		generateSearchPage(tagList,categoryMap,total);
		
		
	}
	
	/* 生成网站siteMap */
	public static generateSiteMap(List blogList,Map categoryMap,List tagList)
	
	{
		def lastModTime=GenUtil.formatDate(new Date(),"yyyy-MM-dd");
		def freq="daily";
		def priority="1";
		def fileName="";

		def fileHeader="""<?xml version="1.0" encoding="UTF-8"?>
		<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">"""
		def fileFooter="""
		</urlset>"""
		def fileBody="";
		def commonfile={		
		return """
		<url>
		<loc>${Config.websiteUrl}${fileName}</loc>
		<lastmod>${lastModTime}</lastmod>
		<changefreq>${freq}</changefreq>
		<priority>${priority}</priority>
		</url>"""		
		}
		
		StringBuffer output=new StringBuffer(fileHeader);
		
		//循环所有分类,生成分类页面

		for(category in categoryMap)
		{
		    fileName="/category/"+category.value+".html";
		    fileBody=commonfile();
		    output.append(fileBody);
		    
		}


		//生成首页
		fileName="/index.html"
		fileBody= commonfile();
		output.append(fileBody);
		
		//生成taglist页
		fileName="/tags/index.html"
		fileBody= commonfile();
		output.append(fileBody);
		
		//生成tag页
		for(tag in tagList)
		{
			 fileName=tag.webPath;
			 lastModTime=tag.lastmod;
			 fileBody=commonfile();
			 output.append(fileBody);
		}
		
		//开始生成detail页面map

		freq="weekly"
		priority="0.8"
			 
		for(blog in blogList)
		{
		 fileName=blog.webPath;
		 lastModTime=blog.simpleDate;
		 fileBody=commonfile();
		 output.append(fileBody);
		}

		output.append(fileFooter);

		def outputFile=new File(Config.sitemapFile);

		outputFile.write(output.toString());

		println "sitemap has generated"
		
		
	}
	
//	 生成detail页面的方法

	private static generateDetailPage(Blog blog,Map categoryMap,int total)
	{
		def template=DataFactory.detailTemplate;	
		def detailBinding = ["blog":blog,"categoryList":categoryMap,"total":total];
		def engine = new SimpleTemplateEngine()
		def detailResult=engine.createTemplate(template).make(detailBinding).toString();
		// print result.toString();
		def outputDetail=new File(blog.filePath);
		outputDetail.write(detailResult);
		
		}

//	 生成首页，分类页面,tag页面的方法
	private static generateListPage(Page page,categoryMap,int total,String category,Tag tag) 
	
	{

		def binding = ["page":page,"categoryList":categoryMap,"total":total];
		def engine = new SimpleTemplateEngine()
		def fileName=page.getUrl(page.getPageNo());
		String template=DataFactory.indexTemplate;

		if(category!=null)
		{ 
		 binding["curretCategory"]=category;	
		 fileName="category"+Config.separator+fileName;
		 template=DataFactory.categoryTemplate;
		 }
		
		if(tag!=null)
		{ 
		 binding["tag"]=tag;	
		 fileName=tag.getFilePath()+fileName;
		 template=DataFactory.tagTemplate;
		 }
		
		
		def templateEngine=engine.createTemplate(template)
		def result=	templateEngine.make(binding).toString();
		
	//	println result;
		
		def outputFile=new File(Config.topFolder+fileName);
		outputFile.write(result);
		}

	//生成taglist页
	private static generateTagListPage(List tagList,Map categoryMap,int total)
	{
		def template=DataFactory.taglistTemplate;	
		def binding = ["tagList":tagList,"categoryList":categoryMap,"total":total];
		def engine = new SimpleTemplateEngine()
		def result=engine.createTemplate(template).make(binding).toString();
		// print result.toString();
		def outputFile=new File(Config.topFolder+Config.folderMap["tags"]+Config.separator+"index.html");
		outputFile.write(result);
		
		}
	
	//生成search页
	private static generateSearchPage(List tagList,Map categoryMap,int total)
	{
		def template=DataFactory.searchTemplate;	
		def binding = ["tagList":tagList,"categoryList":categoryMap,"total":total];
		def engine = new SimpleTemplateEngine()
		def result=engine.createTemplate(template).make(binding).toString();
		// print result.toString();
		def outputFile=new File(Config.topFolder+"search.html");
		outputFile.write(result);
		
		}
	
	
	
  static void main(args) {
    
	  DataFactory.init();
	  generateRss(DataFactory.blogList);
  }

}